(function () {
	/**
	 * Created by Json on 2015/1/8.
	 */
	function onTap( el, func, isStopPropagation ) {
		el.style.setProperty( "cursor", "pointer", null );
		var dx, dy;
		var isTap = true;
		var down = function ( downe ) {
			isStopPropagation && downe.stopPropagation();
			isTap = true;
			dx = downe.pageX;
			dy = downe.pageY;
			el.classList.add( "tap" );
			var move = function ( movee ) {
				if ( movee.pageX - dx > 3 || movee.pageY - dy > 3 ) {
					isTap = false;
				}
			};
			var end = function ( e ) {
				el.classList.remove( "tap" );
				document.removeEventListener( "mousemove", move, false );
				document.removeEventListener( "mouseup", end, false );
				if ( isTap && e.target == el ) {
					func && func();
				}
			};
			document.addEventListener( "mousemove", move, false );
			document.addEventListener( "mouseup", end, false );
		};
		el.addEventListener( "mousedown", down, false );
	}

	function loop( num, func ) {
		for ( var i = 0; i < num; i++ ) {
			func( i );
		}
	}

	function loopArray( array, func ) {
		var len = array.length;
		for ( var i = 0; i < len; i++ ) {
			func( array[i], i );
		}
	}

	// 遍历对象
	function loopObj( obj, block ) {
		for ( var key in obj ) {
			block( key, obj[key] );
		}
	}

	function CSS( el, rules ) {
		loopObj( rules, function ( key, value ) {
			el.style.setProperty( key, value, null );
		} );
	}

	function element( tagName, arg, parentNode ) {
		var el = document.createElement( tagName );
		arg && loopObj( arg, function ( key, value ) {
			switch ( key ) {
				case "classList":
					if ( typeof value == "string" ) {
						el.classList.add( value );
					}
					else {
						loopArray( value, function ( className ) {
							el.classList.add( className );
						} );
					}
					break;
				case "src":
					el.src = value;
					break;
				case "style":
					CSS( el, value );
					break;
				default :
					el.key = value
			}
		} );
		parentNode && parentNode.appendChild( el );
		return el;
	}
    
	var Insert = function ( fatherObj, childObj ) {
		var key;
		for ( key in childObj ) {
			fatherObj[key] = childObj[key];
		}
	};
	var message = function () {
		var msgBox = $( ".fp-msg-box" );
		if ( msgBox.length == 0 ) {
			msgBox = $( "<div class='fp-msg-box'></div>" );
			msgBox.css( {
				position : "absolute",
				top : "45%",
				left : " 46%",
				padding : "15px",
				"max-width" : "560px",
				"min-height" : "15px",
				color : "white",
				"font-size" : "12px",
				"-moz-border-radius" : "4px",
				"line-height" : "150%",
				"border-radius" : "5px",
				"background-color" : "rgba(0, 0, 0, 0.8)",
				"z-index" : "100000",
				"-moz-box-shadow" : "0 2px 4px rgba(0, 0, 0, 0.2)",
				"-webkit-box-shadow" : "0 2px 4px rgba(0, 0, 0, 0.2)",
				"box--shadow" : "0 2px 4px rgba(0, 0, 0, 0.2)"
			} );
			msgBox.hide();
			$( "body" ).append( msgBox );
		}

		return function ( options ) {
			var msg = $( "<div class='fp-msg'></div>" );
			msg.css( {
				position : "relative",
				"margin-bottom" : "10px"
			} );
			msg.addClass( options.type || "" );
			msg.text( options.text );
			msgBox.fadeIn();
			// 先清空
			msgBox[0].innerHTML = "";
			msgBox.append( msg );
			function removeMsg() {
				msgBox.fadeOut( "normal", function () {
					msgBox.html( "" );
				} );
				msgBox.unbind( "click" );
				clearTimeout( timeoutRemove );
			}

			msgBox.bind( "click", removeMsg );
			var timeoutRemove = setTimeout( removeMsg, 3000 );
			return msgBox;
		};
	}();

	window.fp == undefined && (window.fp = {});

	Insert( fp, {
		onTap : onTap,
		loop : loop,
		loopArray : loopArray,
		loopObj : loopObj,
		CSS : CSS,
		element : element,
		message : message
	} );

})();